package com.bree.erp.service.stateless;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.bree.erp.entity.Currency;
import com.bree.erp.service.CurrencyService;

@Stateless
public class CurrencyServiceBean implements CurrencyService {
	
	@PersistenceContext()
	private EntityManager em;
	
	public CurrencyServiceBean() {
		
	}
	
	public CurrencyServiceBean(EntityManager em) {
		this.em = em;
	}

	@Override
	public Currency create(String isoCode, String symbol, String name) {
		
		Currency currency = new Currency();
		currency.setIsoCode(isoCode);
		currency.setSymbol(symbol);
		currency.setName(name);
		
		em.persist(currency);
		
		return currency;
	}

	@Override
	public Currency read(String isoCode) {
		
		return em.find(Currency.class, isoCode);
	}

	@Override
	public Currency update(String isoCode, String symbol, String name) {
		
		Currency currency = read(isoCode);
		currency.setSymbol(symbol);
		currency.setName(name);
		
		return currency;
	}

	@Override
	public void delete(String isoCode) {

		Currency currency = read(isoCode);
		
		em.remove(currency);
		
	}

}
